package com.example.lemis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.common.exception.NotLoginException;
import com.example.lemis.common.exception.PermissionDeniedException;
import com.example.lemis.common.utils.SessionUtils;
import com.example.lemis.model.domain.Admin;
import com.example.lemis.mapper.AdminMapper;
import com.example.lemis.model.dto.AdminLoginRequest;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.AdminInfoVO;
import com.example.lemis.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理系统用户 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-24
 */
@Service
@AllArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    private final AdminMapper adminMapper;
    private final PasswordEncoder passwordEncoder;

    @Override
    public AdminInfoVO login(AdminLoginRequest adminIdentity) {
        String adminId = adminIdentity.getEmployeeNumber();
        String adminPassword = adminIdentity.getPassword();
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getEmployeeNumber, adminId);

        Admin admin = adminMapper.selectOne(queryWrapper);
        if (admin == null) {
            return null; // 登录失败，返回 null
        }

        if (!passwordEncoder.matches(adminPassword, admin.getPassword())) {
            return null; // 密码不匹配，登录失败
        }
        // 登录成功，更新最后登录时间等信息
        Admin updateEntity = new Admin()
                .setId(admin.getId());     // 只需主键
        updateById(updateEntity);

        // 转换为 AdminInfoVO
        AdminInfoVO adminInfo = new AdminInfoVO();
        BeanUtils.copyProperties(admin, adminInfo);   // 字段名 & 类型相同会自动复制

        SessionUtils.saveCurrentAdminInfo(admin);

        return adminInfo;

    }

    @Override
    public AdminInfoVO auth() {
        // 未登录或未找到管理员信息
        Admin admin = SessionUtils.getCurrentAdminInfo();
        if (admin == null) {
            throw new NotLoginException("您还未登录");
        }

        AdminInfoVO adminInfoVO = new AdminInfoVO();
        BeanUtils.copyProperties(admin, adminInfoVO); // 字段名 & 类型相同会自动复制
        return adminInfoVO; // 返回当前管理员信息
    }

    @Override
    public List<String> checkApplyPerm(Long adminId, String permName, boolean needAuth) {
        List<String> perms = adminMapper.selectPermsById(adminId);
        // 检查管理员是否有指定权限
        if (needAuth && (!perms.contains(permName))) {
            throw new PermissionDeniedException("审批权限不足，无法审批该申请");
        }
        return perms;
    }

    @Override
    public Page<AdminInfoVO> getAdminList(PageDTO pageDTO) {
        auth();
        Page<AdminInfoVO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        // 分页查询管理员列表
        return adminMapper.selectAdminList(page);
    }
}
